package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyOrder;
import com.huihuiyun.huihuiyun.server.repository.HhyOrderRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/hhyOrders")
@Slf4j
public class HhyOrderService {
    @Autowired
    private HhyOrderRepository hhyOrderRepository;
    @Context
    private HttpServletResponse httpServletResponse;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyOrder post(HhyOrder hhyOrder) {
        try {
            hhyOrder.id = CommonUtil.createRandomId();
            hhyOrder = hhyOrderRepository.save(hhyOrder);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return hhyOrder;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyOrderRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyOrder put(HhyOrder hhyOrder) {
        try {
            if (CommonUtil.isEmpty(hhyOrder.id)) {
                hhyOrder.id = CommonUtil.createRandomId();
            }
            hhyOrder = hhyOrderRepository.save(hhyOrder);
            return hhyOrder;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyOrder findOne(@PathParam("id") String id) {
        try {
            HhyOrder hhyOrder = hhyOrderRepository.findOne(id);
            if (hhyOrder == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return hhyOrder;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyOrder> findAll() {
        try {
            List<HhyOrder> hhyOrderList = hhyOrderRepository.findAll();
            return hhyOrderList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyOrder> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyOrder> page = hhyOrderRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyOrder> hhyOrderList) {
        try {
            hhyOrderRepository.save(hhyOrderList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findOrderListByMemberIdAndStatus")
    public List<HhyOrder> findOrderListByMemberIdAndStatus(@QueryParam("memberId") String memberId, @QueryParam("status") int status) {
        try {
            List<HhyOrder> hhyOrderList = hhyOrderRepository.findAllByMemberIdAndStatus(memberId, status);
            return hhyOrderList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findOrderListByMemberId")
    public List<HhyOrder> findOrderListByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyOrder> hhyOrderList = hhyOrderRepository.findAllByMemberId(memberId);
            return hhyOrderList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findOrderListBySellerIdAndStatus")
    public Page<HhyOrder> findOrderListBySellerIdAndStatus(@QueryParam("sellerId") String sellerId, @QueryParam("status") int status, @QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyOrder> page = hhyOrderRepository.findAllBySellerIdAndStatus(sellerId, status, pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    public Page<HhyOrder> findAllBySellerIdAndMallType(String sellerId, int mailType, int pageNumber, int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyOrder> page = hhyOrderRepository.findAllBySellerIdAndStatus(sellerId, mailType, pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }
}
